package com.server.orm;

import com.server.game.logic.system.PInitLocalId;
import com.server.util.ConfPropertiesUtil;
import mysql.MySqlMgr;

import java.io.File;

public class XDBManager {

    private static xdb.Xdb orm_xdb;

    public static final int XDB_LOCAL_ID_KEY = 1;

    private static String xdb_xml_file = "example.xdb.xml";

    private static XDBManager instance = new XDBManager();

    public static XDBManager getInstance() {
        return instance;
    }

    public void init() {
        MySqlMgr.GetInstance(ConfPropertiesUtil.getInstance().getProperties());
        final xdb.XdbConf conf = new xdb.XdbConf(xdb_xml_file);
        orm_xdb = xdb.Xdb.getInstance();
        orm_xdb.setConf(conf);
        File inuse = new File("xdb/xdb.inuse");
        if (inuse.exists()) {
            inuse.delete();
        }
        // 强制校验两个配置文件的serverId是否一致
        if (ConfPropertiesUtil.getInstance()._SERVER_ID != conf.getLocalId()) {
            throw new RuntimeException("conf properties serverid not same to xdb.conf.localId");
        }
        //
        if (!orm_xdb.start()) {
            throw new RuntimeException("xdb start fail!");
        }
        new PInitLocalId().submit();
    }

    public void close() {
        orm_xdb.stop();
    }

}
